use strict;
use warnings;
use Math::Calculus::Expression;
use Test::More tests => 3;

BEGIN {
    use_ok 'SPAN::Method::LinearEquation::FixedPoint';
}

my $exp = Math::Calculus::Expression->new;
$exp->addVariable('x');
$exp->setExpression('x^3 + (- 9*x) + 3') or die $exp->getError;
diag( "\nExpression: " . $exp->getExpression );

my $equiv_exp = Math::Calculus::Expression->new;
$equiv_exp->addVariable('x');
$equiv_exp->setExpression('x^3/9 + 1/3') or die $equiv_exp->getError;
diag( "Equivalent expression: " . $equiv_exp->getExpression );

my $expected = 0.337623247380319;
diag( "Expected: x=" . $expected );

my $fixPoint = SPAN::Method::LinearEquation::FixedPoint->new(
    expression => $exp,
    equiv_exp  => $equiv_exp,
    start      => 0.5,
    precision  => 0.0005
);
my $got = $fixPoint->run;
diag( "Got: x=" . $got );

cmp_ok( sprintf( "%.15f", $got ), '==', $expected, 'fixed point run with expression: x^3 - 9*x + 3' );

$exp->setExpression('x^2 + x - 6') or die $exp->getError;
diag("\nExpression: " . $exp->getExpression);

$equiv_exp->setExpression('(6 - x)^1/2') or die $equiv_exp->getError;
diag("Equivalent expression: " . $equiv_exp->getExpression);

$expected = 1.99999999999999;
diag("Expected: x=" . $expected);

$fixPoint->expression($exp);
$fixPoint->equiv_exp($equiv_exp);
$fixPoint->start(1);
$fixPoint->precision(0.0000000000001);
$got = $fixPoint->run;
diag("Got: x=" . $got);

cmp_ok( sprintf("%.14f", $got), '==', $expected, 'fixed point run with expression: x^2 + x - 6' );